package com.tech.zhidengxian.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tech.zhidengxian.model.constant.GoodsState;
import com.tech.zhidengxian.model.dataobject.GoodsDO;
import com.tech.zhidengxian.model.dto.GoodsDTO;
import com.tech.zhidengxian.model.dto.request.GoodsRequest;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

@Mapper
public interface GoodsMapper extends BaseMapper<GoodsDO> {
    default IPage<GoodsDO> pageQueryRecommend(GoodsRequest goodsRequest){
        Page<GoodsDO> page = new Page<>(goodsRequest.getCurrentPage(), goodsRequest.getPageSize());
        LambdaQueryWrapper<GoodsDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsDO::getSchool,goodsRequest.getAddress())
                .eq(GoodsDO::getGoodsState, GoodsState.selling)
                .eq(GoodsDO::getIsDeleted,false)
                .orderByDesc(GoodsDO::getPublishTime);
        return selectPage(page,wrapper);
    }

    default IPage<GoodsDO> pageQueryLocalCity(GoodsRequest goodsRequest){
        Page<GoodsDO> page = new Page<>(goodsRequest.getCurrentPage(), goodsRequest.getPageSize());
        LambdaQueryWrapper<GoodsDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsDO::getCity,goodsRequest.getAddress())
                .eq(GoodsDO::getGoodsState, GoodsState.selling)
                .eq(GoodsDO::getIsDeleted,false)
                .orderByDesc(GoodsDO::getPublishTime);
        return selectPage(page,wrapper);
    }

    default IPage<GoodsDO> pageQuerySchool(GoodsRequest goodsRequest){
        Page<GoodsDO> page = new Page<>(goodsRequest.getCurrentPage(), goodsRequest.getPageSize());
        LambdaQueryWrapper<GoodsDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(GoodsDO::getSchool,goodsRequest.getAddress())
                .eq(GoodsDO::getGoodsState, GoodsState.selling)
                .eq(GoodsDO::getIsDeleted,false)
                .orderByDesc(GoodsDO::getPublishTime);
        return selectPage(page,wrapper);
    }

    default List<GoodsDO> select(){
        LambdaQueryWrapper<GoodsDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(GoodsDO::getPublisherInfo, "岳阳");
        return selectList(wrapper);
    }


    default int updateGoodsCount(GoodsDO goodsDO){
        LambdaUpdateWrapper<GoodsDO> wrapper = new LambdaUpdateWrapper<>();
        LambdaUpdateWrapper<GoodsDO> updateWrapper = wrapper.eq(GoodsDO::getId, goodsDO.getId());
        if (goodsDO.getCollectCount() != null){
            updateWrapper.setSql("collect_count = collect_count+1");
            goodsDO.setCollectCount(null);
        }else if (goodsDO.getBrowseCount() != null){
            updateWrapper.setSql("browse_count = browse_count+1");
            goodsDO.setBrowseCount(null);
        }else if (goodsDO.getCommentCount() != null){
            updateWrapper.setSql("comment_count = comment_count+1");
            goodsDO.setCommentCount(null);
        }
        return update(goodsDO,updateWrapper);
    }

    default IPage<GoodsDO> pageQueryByDetail(GoodsRequest goodsRequest){
        Page<GoodsDO> page = new Page<>(goodsRequest.getCurrentPage(), goodsRequest.getPageSize());
        LambdaQueryWrapper<GoodsDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(GoodsDO::getDetail,goodsRequest.getDetail())
                .eq(GoodsDO::getGoodsState, GoodsState.selling)
                .eq(GoodsDO::getIsDeleted,false)
                .orderByDesc(GoodsDO::getPublishTime);
        return selectPage(page,wrapper);
    }

    List<GoodsDO> queryListByFid(@Param("fansId")Long fansId);

//    default int updateGoods(GoodsDO goodsDO) {
//        return updateById(goodsDO);
//    }

}
